package com.boot.security.server.controller.mffl.MfflContract;

import com.boot.security.server.dao.mffl.MfflContractDao;
import com.boot.security.server.dao.mffl.MfflTypeDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflContract;
import com.boot.security.server.model.mffl.Vo.MfflContractVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.impl.mffl.MfflContract.MfflContractService;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 *合同服务（律师端）
 */
@RestController
@RequestMapping("Lawyer/mfflContracts")
@Api(tags = "合同服务（律师端）")
public class Lawyer_MfflContractController {

    //合同dao
    @Autowired
    private MfflContractDao mfflContractDao;

    //合同service
    @Autowired
    MfflContractService mfflContractservice;

    //服务类型
    @Autowired
    MfflTypeDao mffltypedao;


    @PostMapping(value = "sendMail")
    @ApiOperation("律师确认完成服务，等待用户确认")
    public ReturnBean sendMail(@ApiParam(name = "id",value = "合同id",required = true) @RequestParam Integer id,
                               @ApiParam(name = "laywerConten",value = "律师意见" ,required = false) @RequestParam(required = false) String laywerConten,
                               @ApiParam(name = "lawyerimage",value = "律师意见图片",required = false) @RequestParam(required = false) String lawyerimage){
        if(id == null){
            return new ReturnBean("201","合同id,不能为空");
        }
        /*if(laywerConten == null){
            return new ReturnBean("201","律师意见,不能为空");
        }*/
        return mfflContractservice.LawyerSendMail(id,laywerConten,lawyerimage);
    }

    @GetMapping(value = "Phone")
    @ApiOperation("律师打用户电话")
    public ReturnBean Phone(@ApiParam(name = "id",value = "订单id",required = true)@RequestParam Integer id){
        if (id == null){
            return new ReturnBean("201","订单id");
        }
        return mfflContractservice.Phone(0,id);
    }


    @PostMapping(value = "refuse")
    @ApiOperation(value="律师拒绝接受合同服务")
    public ReturnBean refuse(@ApiParam(name = "id",value = "合同id",required = true)@RequestParam Integer id)throws Exception{
        if(id == null){
            return new ReturnBean("201","合同id不能为空");
        }
        return mfflContractservice.refuse(id);
    }

    @PostMapping(value = "confirm")
    @ApiOperation(value = "律师确认接单")
    public ReturnBean confirm(@ApiParam(value = "合同id",name = "id",required = true)@RequestParam Integer id){
        if(id == null){
            return new ReturnBean("201","合同id不能为空");
        }
        return mfflContractservice.confirm(id);
    }


    /**
     * 按id获取合同
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "按id获取合同")
    public ReturnBean<MfflContractVo> get(@ApiParam(name = "id", value = "合同id", required = true) @PathVariable Integer id) {
        if (id == null) {
            return new ReturnBean("201", "合同id不能为空");
        }
        return mfflContractservice.getIdlawyerletter(id);
    }


    /**
     * 列表
     *
     * @param request
     * @return
     */
    @GetMapping
    @ApiOperation(value = "列表")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "serviceId", value = "4合同审核，5合同定制,不传获取全部", paramType = "query", dataType = "int")})
    public ReturnBean<MfflContract> list(PageTableRequest request) {
        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset不能为空");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit不能为空");
        }

        //查询当前用户的
        SysUser sysUser = UserUtil.getLoginUser();
        HashMap<String, Object> map = new HashMap<>();
        map.put("serviceLawyerId", sysUser.getId());
        map.put("serviceId", request.getParams().get("serviceId"));
        map.put("deleteState", 1);
        request.setParams(map);

        PageTableResponse handle = new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return mfflContractDao.count(request.getParams());
            }
        }, new ListHandler() {
            @Override
            public List<MfflContract> list(PageTableRequest request) {
                return mfflContractDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        List<MfflContract> data = (List<MfflContract>)handle.getData();
        //不显示未支付状态的订单
        for (int i=0;i<data.size();i++){
            if(data.get(i).getPayState() == 0){
                data.remove(i);
            }
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(handle);

        return returnBean;
    }



   /* @Autowired
    private MfflContractDao mfflContractDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public MfflContract save(@RequestBody MfflContract mfflContract) {
        mfflContractDao.save(mfflContract);

        return mfflContract;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public MfflContract update(@RequestBody MfflContract mfflContract) {
        mfflContractDao.update(mfflContract);

        return mfflContract;
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mfflContractDao.delete(id);
    }*/
}
